namespace("mk")
mk.SocketManager = {
  listeners: {},
  socket : null,
  init:function() {
      var context = this;
      if (navigator.userAgent.toLowerCase().indexOf('chrome') != -1) {
          context.socket = io.connect(mk.constants.SOCKETS_URL, {'transports': ['xhr-polling']});
      } else {
          context.socket = io.connect(mk.constants.SOCKETS_URL);
      }
      context.socket.on('connect', function () {
          context.socket.on('message', function (msg) {
              context.onEvent(msg.event, msg);
          });
      });
  },
    addEventListener: function(eventName, clback) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        this.listeners[eventName].push(clback);
    },
    removeEventListener: function(eventName, clback) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        this.listeners[eventName].splice(this.listeners[eventName].indexOf(clback), 1);
    },
    onEvent: function(eventName, obj) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        for(var i=0; i<this.listeners[eventName].length; i++) {
            this.listeners[eventName][i](obj);
        }
    }
    , sendMessage: function(obj) {
     this.socket.json.send(obj);
    }
};
